// pages/admin/user_money_list.js
import { Admin } from '../../model/admin.js';
import { Store } from '../../model/store.js';
var admin = new Admin();
var store = new Store();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    store_id: '',
    hidden: false,
    start_date: '请选择',
    end_date: '请选择',
    store_user_name: '选择店员',
    user_money: [],
    show: false,
    minDate: new Date().getTime() - (86400000 * 31 * 3),
    maxDate: new Date().getTime(),
    storeUser: [],
    storeUserName: [],
    store_name: '',
    id: '',
    sum: 0,
    store_user_id: '',
    showUser: false,
    styleOne: 'margin: 240rpx 30rpx 30rpx;',
    store_id_one: '',
    store_name_one: '请选择店面',
    showNameStatus: false,
    store: [],
  },

  /**
  * 获取全部门店
  */
  getStoreList() {
    store.getStore((res) => {
      var storeNameArr = [];
      res.forEach((item) => {
        storeNameArr.push(item.name)
      })
      this.setData({
        storeNameArr: storeNameArr,
        store: res,
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      store_name: options.name,
      id: options.id
    })
    this.getStoreList()
    this.getUserMoneyList()
    this.getStoreUserList()
  },
  /**
   * 获取店员信息
   */
  getStoreUserList() {
    admin.getStoreUserByStore({ store_id: this.data.id }, (res) => {
      var storeUserName = [];
      res.forEach((item) => {
        storeUserName.push(item.name)
      })
      this.setData({
        storeUserName: storeUserName,
        storeUser: res
      })
    })
  },
  onConfirmOne(event) {
    this.setData({
      store_user_id: this.data.storeUser[event.detail.index].id,
      store_user_name: event.detail.value
    })
  },
  /**
   * 获取全部记录
   */
  getUserMoneyList() {
    wx.showLoading({
      title: '加载中',
    });
    var start_date = ''
    if (this.data.start_date != '请选择') {
      start_date = this.data.start_date
    }
    var end_date = ''
    if (this.data.end_date != '请选择') {
      end_date = this.data.end_date
    }
    admin.getUserMoneyListOne({
      id: this.data.id, 
      store_user_id: this.data.store_user_id,
      start_time: start_date,
      end_time: end_date,
      store_id: this.data.store_id_one
    }, (res) => {
      var sum = 0;
      res.forEach((item)=>{
        sum += Number(item.money)
      })
      this.setData({
        user_money: res,
        sum: sum,
      })
      wx.hideLoading();
    })
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },

  onConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      show: false,
      start_date: this.formatDate(start),
      end_date: this.formatDate(end)
    });
  },
  onClickShow() {
    this.setData({ showUser: true });
  },
  onClickHide() {
    this.setData({ showUser: false });
  },
  onClickShowOne() {
    this.setData({ showNameStatus: true });
  },
  onClickHideOne() {
    this.setData({ showNameStatus: false });
  },
  onConfirmTwo(event) {
    console.log(this.data.store);
    this.setData({
      store_id_one: this.data.store[event.detail.index].id,
      store_name_one: event.detail.value
    })
  },
})